###################################################################################################
################################# Appendix (H) ####################################################
###################################################################################################

#Loading required packages
library(sensemakr)
library(estimatr)
library(miceadds)

#Loading the main dataset
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 

data <- read_dta("data_2rounds.dta")

# Running the sensitivity on the main model using OLS and logged outcome (total violence)
data$logallintimid<- log(data$allintimid+1)
main1 <- lm(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
            + incumbent + nocand_competing + round2+as.factor(governorate), data)

se <- coeftest(main1, vcov. = vcovCL(main1, cluster = data$governorate, type = "HC0"))[, "Std. Error"]


r1 <- sensemakr(estimate=0.70398, se=0.131486396, dof=392, 
                treatment = "mb_dummy",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:3,
                ky = 1:3, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)


r2 <- sensemakr(estimate=-0.23913, se=0.046830661, dof=392, 
                treatment = "newndp",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:3,
                ky = 1:3, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r3 <- sensemakr(estimate=-0.23101, se=0.116414663, dof=392, 
                treatment = "split_ndp",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:3,
                ky = 1:3, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r4 <- sensemakr(estimate=0.04262, se=0.009488724, dof=392,
                treatment = "pct_urban",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:3,
                ky = 1:3, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r5 <- sensemakr(estimate=-0.14806, se=0.066636558, dof=392, 
                treatment = "logprotest",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:3,
                ky = 1:3, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)


########## Table (23)

ovb_minimal_reporting(r1, format = "latex")
ovb_minimal_reporting(r2, format = "latex")
ovb_minimal_reporting(r3, format = "latex")
ovb_minimal_reporting(r4, format = "latex")
ovb_minimal_reporting(r5, format = "latex")


############## Figure (4): Contour plots
main1 <- lm(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
            + incumbent + nocand_competing + round2+as.factor(governorate), data)


r1 <- sensemakr(main1, 
                treatment = "mb_dummy",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:5,
                ky = 1:5, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)


r2 <- sensemakr(main1,
                treatment = "newndp",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:5,
                ky = 1:5, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r3 <- sensemakr(main1, 
                treatment = "split_ndp",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:5,
                ky = 1:5, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r4 <- sensemakr(main1,
                treatment = "pct_urban",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:5,
                ky = 1:5, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)

r5 <- sensemakr(main1, 
                treatment = "logprotest",
                benchmark_covariates = list("Candidates No."="nocand_competing"),
                kd = 1:5,
                ky = 1:5, 
                q = 1,
                alpha = 0.05, 
                reduce = TRUE)



png("contour_r1.png")
plot(r1)
dev.off()

png("contour_r2.png")
plot(r2)
dev.off()

png("contour_r3.png")
plot(r3)
dev.off()

png("contour_r4.png")
plot(r4)
dev.off()

png("contour_r5.png")
plot(r5)
dev.off()